<?php

class SelfIndexController extends BaseController {

    static $view_path = 'self_service_end';
    private $foodService = null;
    private $memberService = null;
    private $orderService = null;

    public function doCarousel(){
//        $foodService = $this->getFoodService();
        $memberService = $this->getMemberService();
        $orderService = $this->getOrderService();

//        $top3Foods = $foodService->getList(array('is_top'=>1, 'order'=>'updated_at'), 3);
        $carousels = Carousel::where('status', Carousel::getStatusDisplayNameByKeyword(Carousel::SHOW))->orderBy('updated_at', 'desc')->limit(10)->get();
        $doOrderFoodList = $orderService->getFoodRankingListForMostOrder(10);
        $doSettleUserList = $memberService->getRankingListForSettle(10);

        return View::make(self::$view_path.'.carousel', compact('carousels', 'doOrderFoodList', 'doSettleUserList'));
    }

    public function doLogin(){

        $userId = Input::get('user_id');
        $token = Input::get('token');

        $memberService = new SelfMemberService();
        $repo = App::make('UserRepository');
        $input = $memberService->getUserInfo($userId, $token);

        $result = true;
        $loginMsg = 'success';

        if (!$repo->login($input)) {
            if($userId !== null && !$memberService->checkSignature($userId, $token)){
                $loginMsg = '用户认证失败';
                $result = false;
            }else {
                $userInfo = $memberService->createMember();
                if($userInfo === false){
                    $loginMsg = '新用户创建失败'.var_dump($memberService->errors()->getMessages());
                    $result = false;
                }

                if ($repo->login($userInfo)) {
                    $loginMsg = 'success';
                    $result = true;
                }
            }
        }

        BLogger::getLogger(BLogger::LOG_LOGIN)->info('Log in From Self Service Device, UserID:'.$userId.', token:'.$token.', result:'.$loginMsg);
        if($result){
            return Redirect::to('self_service_end/index?clear_cache=1');
        }
        echo $loginMsg;
    }

    /**
     * 后台登出操作
     */
    public function doLogout(){

        Confide::logout();

        return Redirect::action('BackIndexController@doLogin')
            ->with('success', '注销成功!');
    }

    /**
     * 获取订单信息，返回 JSON 格式
     * @return mixed
     */
    public function getOrderPicket()
    {
        $service = $this->getOrderService();
        $checksum = Input::get('checksum');
        $timestamp = Input::get('timestamp');
        $order_id = Input::get('order_id');
        $result = $service->getPicket($order_id, $checksum, $timestamp);
        if(!$result) {
            return json_encode(array('errmsg' => $service->errors()->first()));
        }
        return json_encode($result);
    }

    /**
     * 设置订单已结算状态
     * @return mixed
     */
    public function setOrderSettled(){
        $service = $this->getOrderService();
        $checksum = Input::get('checksum');
        $timestamp = Input::get('timestamp');
        $order_id = Input::get('order_id');
        $result = $service->setOrderStatus($order_id, $checksum, $timestamp, Order::SETTLED);
        if(!$result) {
            return json_encode(array('result'=>false,'msg' => $service->errors()->first()));
        }
        return json_encode(array('result'=>true,'msg' => '设置结算状态成功','order_id'=>$order_id));
    }

    /**
     * 设置订单已出单状态
     * @return mixed
     */
    public function setOrderPrinted(){
        $service = $this->getOrderService();
        $checksum = Input::get('checksum');
        $timestamp = Input::get('timestamp');
        $order_id = Input::get('order_id');
        $result = $service->setOrderStatus($order_id, $checksum, $timestamp, Order::GOT_PICKET);
        if(!$result) {
            return json_encode(array('result'=>false,'msg' => $service->errors()->first()));
        }
        return json_encode(array('result'=>true,'msg' => '设置出单状态成功','order_id'=>$order_id));
    }

    public function getFoodService() {
        if(is_null($this->foodService)){
            $this->foodService = new SelfFoodService();
        }
        return $this->foodService;
    }

    public function getMemberService() {
        if(is_null($this->memberService)){
            $this->memberService = new SelfMemberService();
        }
        return $this->memberService;
    }

    public function getOrderService() {
        if(is_null($this->orderService)){
            $this->orderService = new SelfOrderService();
        }
        return $this->orderService;
    }
    //判断订单是否支付
    public function getIsSettled(){
        $order = Order::find(Input::get('order_id',-1));
        if($order){
            if($order->status==2){
                return json_encode(array('result'=>true,'msg' => '已经支付','order_id'=>$order->id));
            }
        }
        return json_encode(array('result'=>false,'msg' => '未支付','order_id'=>$order->id));

    }
}
